<template>
  <div class="indexTable bgWhite productBasic" id="instructions" ref="content">
   <el-form ref="form" class="pb_form">
      <el-select  v-model="searchForm.instructions_type.value" placeholder="说明书类型" filterable clearable 
         @keyup.enter.native="getTableList" @focus="getInstructionsList" >
        <el-option v-for="item in instructionsList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input placeholder="适用产品" clearable v-model="searchForm.applicable_product.value" @keyup.enter.native="getTableList"></el-input>
      <div class="createTime">
         <el-input v-model="update_time" placeholder="操作时间"></el-input>
         <el-date-picker @change="changeUpdateTime"   v-model="update_timePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
      <el-input placeholder="创建人" clearable v-model="searchForm.create_name.value" @keyup.enter.native="getTableList"></el-input>
    </el-form>
    <div class="btnContainer" ref="btn">
      <div>
        <el-button type="search" @click="closeAddDialog" v-if="$_has('/instructions/add')">添加</el-button>
        <el-button type="del" @click="closeImportDialog()" v-if="$_has('/instructions/import')">导入</el-button>
        <el-button type="del" @click="sureCloseDialog('del')" v-if="$_has('/instructions/del')">删除</el-button>
        <el-button type="del" @click="closeExportDialog()" v-if="$_has('/instructions/export')">导出</el-button>
      </div>
      <div>
        <el-button type="search" @click="getTableList" v-if="$_has('/instructions/list')">查询</el-button>
        <el-button type="del" @click="resetForm">重置</el-button>
      </div>
    </div>
    <el-table :data="listTable" :height="tableH" :row-class-name="rowClassName" ref="table" @row-click="RowClick"
     @selection-change="handleSelectionChange" :row-key="function(row){return row.id}" v-loading="loading">
      <el-table-column type="selection" width="64"  fixed="left" :reserve-selection="true"></el-table-column>
      <el-table-column  label="说明书类型"  prop="instructions_type" align="left"> </el-table-column>
      <el-table-column  label="适用产品" prop="applicable_product" align="left"></el-table-column>
      <el-table-column  label="说明书标题"   prop="instructions_title" align="left"></el-table-column>
      <el-table-column  label="连接示意图"   prop="connection_diagram" align="left" > 
        <template slot-scope="scope" >
          <el-image  :src="utils.setCdnUrl() + scope.row.connection_diagram" v-if="scope.row.connection_diagram" class="connection_diagram_table" :preview-src-list="scope.row.array_image"></el-image>
        </template>
      </el-table-column>
      <el-table-column  label="说明书详情" >
        <template slot-scope="scope" >
          <i class="iconfont  icon-details_normal" @click.stop="setCurRow(scope.row, 'detail')"></i>
        </template>
      </el-table-column>
      <el-table-column  label="操作时间" width="200"  prop="update_time"></el-table-column>
      <el-table-column  label="创建人"  width="120"    prop="create_name"></el-table-column>
      <el-table-column  label="操作" fixed="right"  width="120"  class-name="options">
        <template slot-scope="scope" >
          <i class="iconfont  icon-edit_normal"  v-if="$_has('/instructions/edit')" @click.stop="setCurRow(scope.row, 'edit')"></i>   
          <i class="iconfont  icon-delete_normal" v-if="$_has('/instructions/del')" @click.stop="setCurRow(scope.row, 'del')" ></i>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination ref="pagination" background :page-sizes="pageSizes" :total="total"  :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"  @size-change="handleSizeChange" :current-page="page"
      @current-change="handleCurrentChange"></el-pagination>
    <Add v-show="addShow" :show="addShow" @closeDialog="closeAddDialog"></Add>
    <Edit v-show="editShow" :id="curId" :show="editShow" @closeDialog="closeEditDialog"></Edit>
    <Del v-show="delShow" :idList="curList" :show="delShow" @closeDialog="closeDelDialog"></Del>
    <Export v-show="exportShow" :show="exportShow" @closeDialog="closeExportDialog"></Export>
    <Detail v-show="detailShow" :id="curId" :show="detailShow" @closeDialog="closeDetailDialog"></Detail>
    <Import v-show="importShow" :show="importShow" @closeDialog="closeImportDialog"></Import>

  </div>
</template>

<script>
import { Add, Edit, Del, Export, Detail, Import } from './components'
import { list ,typeList } from '@/api/product/instructions.js'
import { setSearchParams } from '@/utils/index.js'
export default {
  name: 'instructions',
  data() {
    return {
      searchForm: {
        instructions_type: { value: '', op: '=' },
        applicable_product: { value: '', op: 'like' },
        update_time: { value: '', op: 'RANGE' },
        create_name:{ value: '', op: 'like' }
      },
      listTable: [  ],
      pageSizes: [20, 50, 100],
      total: 10,
      pageSize: 20,
      page: 1,
      addShow: false,
      editShow: false,
      delShow: false,
      exportShow: false,
      tagShow: false,
      imageTextShow: false,
      specShow: false,
      detailShow: false,
      importShow: false,
      curList: [],
      curId: null,
      tableH: null,
      curRow: null,
      curIdList: [],
      selectList: [],
      loading: false,
      productNameList: [],//产品编码下拉框
      instructionsList: [], //说明书名称
      update_timePicker: '',
      update_time: ''

    }
  },
  created(){
    this.getTableList()
  },
  mounted(){
    let that = this;
    that.setTableHeight()
    // 注：window.onresize只能在项目内触发1次
    window.onresize = function windowResize () {
        // 通过捕获系统的onresize事件触发我们需要执行的事件
      that.setTableHeight()
    }
  },
  methods: {
    getInstructionsList() { //说明书类型 下拉框
      typeList().then(res=>{
        this.instructionsList = res.data
      })
    },
    closeDetailDialog() {
      this.detailShow = !this.detailShow
    },
    closeImportDialog(data) {
      this.importShow = !this.importShow
      if(data) {
        this.getTableList()
      }
    },
    changeUpdateTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.update_time = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.update_time.value = str
      }else{
        this.searchForm.update_time.value = null
      }
      this.getTableList()
    },
    RowClick (row) {
      this.$refs.table.toggleRowSelection(row);
    } ,
    setTableHeight(){
      let searchFormH = this.$refs.form.$el.offsetHeight;
      let manageMenuH = this.$refs.content.offsetHeight;
      let btnContainerH = this.$refs.btn.offsetHeight;
      let paginationH = this.$refs.pagination.$el.offsetHeight
      let tableHeight = manageMenuH  - btnContainerH - searchFormH - paginationH - 48 - 24 - 24
      this.tableH = tableHeight +'px' 
    },
    sureCloseDialog(type){
 
      if(this.curList.length > 0){
        if(type == 'del'){
          this.closeDelDialog()
        } else  if(type == 'tag'){
          this.closeTagDialog()
        } 
      } else {
        let word = "删除"
        if(type == 'tag'){
          word = '打标签'
        }
        this.$message.error('请选择需' + word + '的选项！')
      }
      
    },
    getTableList(){
      this.loading = true
      let params = setSearchParams(this.searchForm, this.pageSize, this.page)
      list(params).then(res=>{
        let list = res.data.data
        if( list.length == 0 && this.page > 1){
          this.page -= 1
          this.getTableList()
        }
        let  idList = []
        for(let i = 0; i < list.length; i++){
          if(list[i].status == 1) {
            list[i].statusBoolean = true
          } else {
            list[i].statusBoolean = false
          }
          list[i].array_image = [ this.utils.setCdnUrl() + list[i].connection_diagram]
          idList.push(list[i].product_id)
        }
        this.curIdList = idList

        this.listTable = list
        this.total = res.data.total
        this.page = res.data.current_page
        this.$refs.table.clearSelection()
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableList()
     
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableList()
      this.$refs.table.clearSelection()
    },
    closeAddDialog(){
      this.addShow = !this.addShow
      if(!this.addShow){
        this.getTableList()
      }
    },
    setCurRow(row, type) {
      this.curList = []
      this.curList.push(row.product_id)
      this.curId = row.product_id
      this.curRow = row
      if(type == 'edit'){ 
        this.closeEditDialog()  
      }else if(type == 'del'){
        this.closeDelDialog()
      } else if(type == 'detail'){
        this.closeDetailDialog()
      }
      
    },
    closeEditDialog(){
      this.editShow = !this.editShow
      if(!this.editShow){
        this.getTableList()
      }
    },
    closeDelDialog(){
      this.delShow = !this.delShow
      if(!this.delShow){
        this.getTableList()
      }
    },
    closeExportDialog(){
      this.exportShow = !this.exportShow
      if(!this.exportShow){
        this.getTableList()
      }
    },
    rowClassName({row}){
      let rowClass = "tableRowClass"

      if(row.statusSelection){
        rowClass += ' selectRow'
      }
      return rowClass
    },
    handleSelectionChange(list){
      let array = []
      for(let i = 0; i < list.length; i++ ){
        array.push(list[i].product_id)
      }
      this.curList = array

      // 选中行的样式赋值
      for(let i in this.selectList){
        this.selectList[i].statusSelection = false
      }
      for (let item in list){
        list[item].statusSelection = true
      }
      this.selectList = list
    },

     // 重置按钮
    resetForm(){
      for(let item in this.searchForm){
        this.searchForm[item].value =""
      }
      this.update_time =''
      this.getTableList()
    },
  },
  components: { Add, Edit, Del, Export, Detail, Import }

}
</script>

<style lang="scss">
@import '@/styles/product/instructions.scss';

</style>